/**
 * 
 */
package sopia.sg;

import java.util.Calendar;

import sopia.conn.DBAdapter;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * @author tatsu@gmail.com
 *
 */
public class G004Activity extends Activity {
	 private int myYear, myMonth, myDay;
	 private String forMonth,forMonthToday;
	 private Button datePickerButton;
	 static final int ID_DATEPICKER = 0;
	    private Button.OnClickListener datePickerButtonOnClickListener
	    = new Button.OnClickListener(){

	  @Override
	  public void onClick(View v) {
	   // TODO Auto-generated method stub
	   final Calendar c = Calendar.getInstance();
	   myYear = c.get(Calendar.YEAR);
	   myMonth = c.get(Calendar.MONTH);
	   myDay = c.get(Calendar.DAY_OF_MONTH);
	   showDialog(ID_DATEPICKER);
	  }
	   };
	 private DatePickerDialog.OnDateSetListener myDateSetListener
	  = new DatePickerDialog.OnDateSetListener(){
	   @Override
	   public void onDateSet(DatePicker view, int year, 
	     int monthOfYear, int dayOfMonth) {
	    // TODO Auto-generated method stub
	    String date = "Year: " + String.valueOf(year) + "\n"
	     + "Month: " + String.valueOf(monthOfYear+1) + "\n"
	     + "Day: " + String.valueOf(dayOfMonth);
//	    Toast.makeText(G004Activity.this, date, 
//	      Toast.LENGTH_LONG).show();
	    forMonth = String.valueOf(monthOfYear + 1) + "/" + String.valueOf(year);
	    datePickerButton.setText(forMonth);
	   } 
	 };
	/**
	 * 
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.g004_layout);
		DBAdapter db = new DBAdapter(this);
		datePickerButton = (Button)findViewById(R.id.datepicker_button);
        datePickerButton.setOnClickListener(datePickerButtonOnClickListener);
        init();
	}


   
	private void init() {
		//init time
		   final Calendar cl = Calendar.getInstance();
		   myYear = cl.get(Calendar.YEAR);
		   myMonth = cl.get(Calendar.MONTH);
		   forMonthToday = String.valueOf(myMonth + 1) + "/" + String.valueOf(myYear);
		   datePickerButton.setText(forMonthToday);
		   //myDay = c.get(Calendar.DAY_OF_MONTH);
		   
		//init spinner
				Spinner categoryCombobox = (Spinner) findViewById(R.id.category_combobox);
				ArrayAdapter adapter = ArrayAdapter.createFromResource(
				            this, R.array.category, android.R.layout.simple_spinner_item);
			    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			    categoryCombobox.setAdapter(adapter);
	    // screen ID call
		Intent receiveIntent = this.getIntent();
		final String screenID = receiveIntent.getStringExtra("ID");
	    ///back screen
		final ImageButton backImagebutton = (ImageButton) findViewById(R.id.back_imagebutton);
		backImagebutton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent i = null;
				if("G002".equals(screenID)){
					 i = new Intent(G004Activity.this, G002Activity.class);
				} else if ("G005".equals(screenID)){
					 i = new Intent(G004Activity.this, G005Activity.class);
				}else {
					 i = new Intent(G004Activity.this, G002Activity.class);
				}
//				i.putExtra("ID", "G004");
//				Bundle sendBundle = new Bundle();
//				sendBundle.putLong("value", receiveValue);
//				i.putExtras(sendBundle);
				startActivity(i);
			}
		});		//
		final Button forward_addCategory_button = (Button) findViewById(R.id.forward_addCategory_button);
		forward_addCategory_button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(G004Activity.this, G007Activity.class);
				i.putExtra("ID", "G004");
				startActivity(i);
			}
		});
	    
	}
	@Override
	 protected Dialog onCreateDialog(int id) {
	  // TODO Auto-generated method stub
	  switch(id){
	   case ID_DATEPICKER:
//	    Toast.makeText(G004Activity.this, 
//	      "- onCreateDialog -", 
//	      Toast.LENGTH_LONG).show();
	    return new DatePickerDialog(this,
	      myDateSetListener,
	      myYear, myMonth, myDay);
	   default:
	    return null;
	  }
	 }
}
